﻿using Modules;
using Modules.Base;
using Modules.CheckIn.Entitys;
using Modules.Person;
using Modules.Project;
using Modules.Project.Entitys;
using Org.BouncyCastle.Utilities.Encoders;
using PmSoft;
using PmSoft.FileStorage;
using static Modules.CheckIn.CheckInDetailConstants;

namespace AppWebApi.Models.Dtos
{
    public class CheckInDetailDto
    {
        /// <summary>
        /// 主键 
        ///</summary>
        public string Id { get; set; }
        /// <summary>
        /// 人员名称 
        ///</summary>
        public string? PersonName { get; set; }
        /// <summary>
        /// 考勤状态  
        ///</summary>
        public string? StatusName { get; set; }
        /// <summary>
        /// 进出标识  
        ///</summary>
        public 进出标识? EntryExitMark { get; set; }
        /// <summary>
        /// 进出标识  
        ///</summary>
        public string? EntryExitMarkName { get; set; }
        /// <summary>
        /// 数据来源 
        ///</summary>
        public 数据来源? DataSources { get; set; }
        /// <summary>
        /// 数据来源 
        ///</summary>
        public string? DataSourcesName { get; set; }
        /// <summary>
        /// 班组名称
        /// </summary>
        public string? GroupName { get; set; }
        /// <summary>
        /// 开始用工日期
        /// </summary>
        public DateTime? StartDate { get; set; }
        /// <summary>
        /// 结束用工日期
        /// </summary>
        public DateTime? EndDate { get; set; }

        /// <summary>
        /// 考勤日期 
        ///</summary>
        public DateTime? CheckinDate { get; set; }
        /// <summary>
        /// 创建时间 
        ///</summary>
        public DateTime? CreateTime { get; set; }
        /// <summary>
        /// 时间戳
        /// </summary>
        public long TimeStamp { get; set; }

        /// <summary>
        /// 设备编码
        /// </summary>
        public string? EquipmentCode { get; set; }

        #region 扩展字段
        /// <summary>
        /// 考勤照片
        /// </summary>
        public string Base64PhotoStr { get; set; }
        #endregion
    }

    public static partial class CheckInDetailExceptions
    {

        public static IEnumerable<CheckInDetailDto> AsAppDtos(this IEnumerable<CheckInDetail> data)
        {
            List<CheckInDetailDto> dtos = new();
            var personService = ServiceAccessor.GetService<PersonService>();
            var persons = personService.GetPersonsByPersonIds(data.Select(a => a.PersonId));
            foreach (var item in data)
            {
                CheckInDetailDto dto = item.AsAppDto(persons.FirstOrDefault(a => a.PersonId == item.PersonId));
                dtos.Add(dto);
            }
            return dtos;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="entity"></param>
        /// <param name="personInfo"></param>
        /// <returns></returns>
        public static CheckInDetailDto AsAppDto(this CheckInDetail entity, PersonInfo? personInfo)
        {
            CheckInDetailDto dto = new CheckInDetailDto();
            dto.Id = entity.Id;
            dto.StatusName = entity.Status.GetDescription();
            dto.EntryExitMarkName = entity.EntryExitMark.GetDescription();
            dto.DataSourcesName = entity.DataSources.GetDescription();
            dto.CheckinDate = entity.CheckinDate;
            dto.CreateTime = entity.CreateTime;
            dto.EquipmentCode = entity.EquipmentCode;
            dto.TimeStamp = entity.CreateTime.ToTimeStamp();
            dto.PersonName = personInfo?.Name;

            return dto;
        }

        public static CheckInDetailDto AsAppDetailDto(this CheckInDetail entity)
        {
            var personService = ServiceAccessor.GetService<PersonService>();
            var persons = personService.GetPerson(entity.PersonId);
            CheckInDetailDto dto = entity.AsAppDto(persons);
            var projectGroupService = ServiceAccessor.GetService<ProjectGroupService>();
            ProjectGroup? projectGroup = projectGroupService.GetProjectGroup(entity.GroupId);
            var fileStorage = ServiceAccessor.GetService<IFileStorage>();
            byte[] objectData = fileStorage.GetFileAsync(FileBuckets.Checkin, entity.FacePath).GetAwaiter().GetResult();
            if (objectData.Length > 0)
                dto.Base64PhotoStr = Base64.ToBase64String(objectData);
            dto.GroupName = projectGroup?.Name;
            return dto;
        }

    }
}
